/*
 * Author: 李波(Edwin Lee)
 * Date: 2024-08-10 16:22:32
 * FilePath: /mprpc/src/public.cc
 * LastEditTime: 2024-08-23 05:19:44
 * version: 1.0
 * Description: 公共函数
 */
#include "public.h"

bool strTrim(char* strOut, const char* strIn) noexcept
{
	if (NULL == strOut || NULL == strIn)
		return false;
	int lenth = strlen(strIn);
	int begin = 0;
	int end = lenth - 1;
	for (; begin < lenth; ++begin)
	{
		if (strIn[begin] != ' ' && strIn[begin]!='\t')
			break;
	}

	for (; end >= begin; --end)
	{
		if ('\n' != strIn[end] && '\t' != strIn[end] && ' ' != strIn[end] && '\r' != strIn[end])
			break;
	}
	strncpy(strOut, strIn + begin, end - begin + 1);
	strOut[end - begin + 1] = 0;//由于strncpy没有碰到'\0'复制后结尾没有0，此处需要补0
	return true;
}


void strTrim(std::string& str) noexcept
{
	size_t lenth = str.size();
	if (0 == lenth)
		return;
	size_t begin = 0;
	size_t end = lenth-1;
	for (; begin < lenth; ++begin)
	{
		if (str[begin] != ' ' && str[begin] != '\t')
			break;
	}

	for (; end >= begin; --end)
	{
		if ('\n' != str[end] && '\t' != str[end] && ' ' != str[end] && '\r' != str[end])
			break;
	}
    str = std::string(str, begin, end - begin + 1);
	return;
}

void pathTrim(char* pathOut, const char* pathIn) noexcept
{
	strTrim(pathOut, pathIn);
	for (size_t i = 0; i < strlen(pathOut); ++i)
	{
		if (pathOut[i] == '\\')
			pathOut[i] = '/';
	}
}

//***************************************LTime&LTimer******************************************

LTime::LTime():isCurrentTime_(true),tm_(){}

LTime::LTime(const time_t* time):isCurrentTime_(false)
{
	SLocaltime(time, &tm_);
}

LTime::~LTime(){}

void LTime::setTimeNow()
{
	isCurrentTime_ = true;
}

void LTime::setTime(const time_t* time)
{
	SLocaltime(time,&tm_);
	isCurrentTime_ = false;
}

std::string LTime::getDate(const char* fmt)
{
	char buf[32] = { 0 };
	if (isCurrentTime_)
	{
		getCurrentTime();
	}
	if (nullptr == fmt || 0 == strcmp(fmt, "yyyy-mm-dd hh24:mi:ss"))
	{
		snprintf(buf,20, "%04d-%02d-%02d %02d:%02d:%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday, tm_.tm_hour, tm_.tm_min, tm_.tm_sec);
	}
	else if (0 == strcmp(fmt, "yyyy-mm-dd hh24:mi"))
	{
		snprintf(buf, 17, "%04d-%02d-%02d %02d:%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday, tm_.tm_hour, tm_.tm_min);
	}
	else if (0 == strcmp(fmt, "yyyy-mm-dd hh24"))
	{
		snprintf(buf, 14, "%04d-%02d-%02d %02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday, tm_.tm_hour);
	}
	else if (0 == strcmp(fmt, "yyyy-mm-dd"))
	{
		snprintf(buf, 11, "%04d-%02d-%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday);
	}
	else if (0 == strcmp(fmt, "yyyy-mm"))
	{
		snprintf(buf, 8, "%04d-%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1);
	}
	else if (0 == strcmp(fmt, "yyyymmddhh24miss"))
	{
		snprintf(buf, 15, "%04d%02d%02d%02d%02d%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday, tm_.tm_hour, tm_.tm_min, tm_.tm_sec);
	}
	else if (0 == strcmp(fmt, "yyyymmddhh24mi"))
	{
		snprintf(buf, 13, "%04d%02d%02d%02d%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday, tm_.tm_hour, tm_.tm_min);
	}
	else if (0 == strcmp(fmt, "yyyymmddhh24"))
	{
		snprintf(buf, 11, "%04d%02d%02d%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday, tm_.tm_hour);
	}
	else if (0 == strcmp(fmt, "yyyymmdd"))
	{
		snprintf(buf, 9, "%04d%02d%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday);
	}
	else if (0 == strcmp(fmt, "hh24miss"))
	{
		snprintf(buf, 7, "%02d%02d%02d"
			, tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday);
	}
	return buf;
}

std::string LTime::getYear()
{
	if (isCurrentTime_)
		getCurrentTime();
	return std::to_string(tm_.tm_year + 1900);
}

std::string LTime::getMonth()
{
	char buf[4]{ 0 };
	if (isCurrentTime_)
		getCurrentTime();
	snprintf(buf, 3, "%02d", tm_.tm_mon + 1);
	return buf;
}

std::string LTime::getDay()
{
	char buf[4]{ 0 };
	if (isCurrentTime_)
		getCurrentTime();
	snprintf(buf, 3, "%02d", tm_.tm_mday);
	return buf;
}

std::string LTime::getHour()
{
	char buf[4]{ 0 };
	if (isCurrentTime_)
		getCurrentTime();
	snprintf(buf, 3, "%02d", tm_.tm_hour);
	return buf;
}

std::string LTime::getMinute()
{
	char buf[4]{ 0 };
	if (isCurrentTime_)
		getCurrentTime();
	snprintf(buf, 3, "%02d", tm_.tm_min);
	return buf;
}

std::string LTime::getSeconds()
{
	char buf[4]{ 0 };
	if (isCurrentTime_)
		getCurrentTime();
	snprintf(buf, 3, "%02d", tm_.tm_sec);
	return buf;
}

void LTime::getCurrentTime()
{
	time_t t;
	time(&t);
	SLocaltime(&t,&tm_);
}

bool createDir(const char* path, bool isFileName)
{
	int len = strlen(path);
	if (len >= MAX_DIR_LENGTH)
		return false;
	char tmppath[MAX_DIR_LENGTH];
	char dir[MAX_DIR_LENGTH] = { 0 };
	pathTrim(tmppath, path);
	len = strlen(tmppath);
	for (int i = 1; i < len; ++i)
	{
		if (path[i] != '/')
			continue;
		strncpy(dir, tmppath, i);
		dir[i] = 0;
		if (0 == access(dir, 0))
			continue;
		if (0 != MKDIR(dir))
			return false;
	}
	if (!isFileName)
	{
		if (0 != access(dir, 0))
		{
			if (0 != MKDIR(dir))
				return false;
		}
	}
	return true;
}

FILE* FOPEN(const char* filename, const char* mode)
{
	if ('w' == mode[0] || 'a' == mode[0])
	{
		if (!createDir(filename, true))
			return nullptr;
	}
	return fopen(filename, mode);

}

bool RENAME(const char* oldpath, const char* newpath, bool isFileName, const int times)
{
	if (0 != access(oldpath, 0))
		return false;
	if (!createDir(newpath, isFileName))
		return false;
	for (int i = 0; i < times; ++i)
	{
		if (0 == rename(oldpath, newpath))
			return true;
		SLEEP(100);
	}
	return false;
}